package com.ext.replacedemo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Set;

/**
 * 优先级：局部替换-->判断忽略行-->整行替换
 * <p>contains匹配到key后，通过String.replace将key替换成value，Key要尽可能的精确，避免误替换。
 * <p>若替换后的行仍然包含原Key，那么key应更精确，或者使用整行替换Gradle_ReplaceLine，并添加完成标志。
 */
public class Gradle_Replace {
    public ArrayList<String> sGradleKeyList = new ArrayList<>();
    public ArrayList<String> sGradleValueList = new ArrayList<>();

    private LinkedHashMap<String, String> mGradleHashMap = new LinkedHashMap<>();

    public Gradle_Replace(boolean migrateTo34, boolean migrateTo35, boolean migrateToX) {
        if (migrateTo34) {
            load34();
        }
        if (migrateTo35) {
            load35();
        }
        if (migrateToX) {
            loadAndroidX();
        }
        if (mGradleHashMap.size() > 0) {
            Set<String> keySet = mGradleHashMap.keySet();
            Collection<String> values = mGradleHashMap.values();
            sGradleKeyList.addAll(Arrays.asList(keySet.toArray(new String[]{})));
            sGradleValueList.addAll(Arrays.asList(values.toArray(new String[]{})));
        }
    }

    private void load34() {
        mGradleHashMap.put("compile ", "api ");
        mGradleHashMap.put("compile(", "api(");
        mGradleHashMap.put("testCompile", "testImplementation");
        mGradleHashMap.put("androidTestCompile", "androidTestImplementation");

        mGradleHashMap.put("return \"git rev-parse --short HEAD\".execute().text.trim()",
                "return \"git rev-parse HEAD\".execute().text.substring(0, 8)");
    }

    private void load35() {

    }

    private void loadAndroidX() {
        mGradleHashMap.put("cardappDependencies.basemoudleX", "cardappDependencies.baseModuleX");
        mGradleHashMap.put("cardappDependencies.basicfunX", "cardappDependencies.basicFunX");
        mGradleHashMap.put("cardappDependencies.captchautilsX", "cardappDependencies.captchaUtilsX");

        mGradleHashMap.put("cardappDependencies.changeskinX", "cardappDependencies.changeSkinX");
        mGradleHashMap.put("cardappDependencies.l_changeskinX", "cardappDependencies.changeSkinX");
        mGradleHashMap.put("cardappDependencies.l_changeSkinX", "cardappDependencies.changeSkinX");

        mGradleHashMap.put("cardappDependencies.swiperefreshandloadmoreX", "cardappDependencies.swipeRefreshAndLoadMoreX");

        mGradleHashMap.put("cardappDependencies.multiimageselectorX", "cardappDependencies.imageSelectorX");
        mGradleHashMap.put("cardappDependencies.imageselectorX", "cardappDependencies.imageSelectorX");

        mGradleHashMap.put("cardappDependencies.ciccoinX", "cardappDependencies.cicCoinX");
        mGradleHashMap.put("cardappDependencies.WebPayX", "cardappDependencies.webPayX");
        mGradleHashMap.put("cardappDependencies.l_pdfX", "cardappDependencies.pdfX");
        mGradleHashMap.put("cardappDependencies.l_paymentX", "cardappDependencies.paymentX");
        mGradleHashMap.put("cardappDependencies.l_ecommerceX", "cardappDependencies.ecommerceX");
        mGradleHashMap.put("cardappDependencies.l_jpushX", "cardappDependencies.jpushX");
        mGradleHashMap.put("cardappDependencies.rxpermissionsX", "cardappDependencies.rxPermissionsX");

        mGradleHashMap.put("cardappDependencies.InboxModuleX", "cardappDependencies.inboxModuleX");
        mGradleHashMap.put("cardappDependencies.hkInboxModuleX", "cardappDependencies.inboxModuleX");

        mGradleHashMap.put("cardappDependencies.asputilsX", "cardappDependencies.aspUtilsX");
        mGradleHashMap.put("cardappDependencies.visitorregisterX", "cardappDependencies.visitorRegisterX");

        mGradleHashMap.put("cardappDependencies.cbMerchantModuleX", "cardappDependencies.merchantModuleX");
        mGradleHashMap.put("cardappDependencies.cbMerchantX", "cardappDependencies.merchantModuleX");
        mGradleHashMap.put("cardappDependencies.hkMerchantModuleX", "cardappDependencies.merchantModuleX");
        mGradleHashMap.put("cardappDependencies.MerchantModuleX", "cardappDependencies.merchantModuleX");

        mGradleHashMap.put("cardappDependencies.hkNecessityModuleX", "cardappDependencies.necessityModuleX");
        mGradleHashMap.put("cardappDependencies.NecessityModuleX", "cardappDependencies.necessityModuleX");
    }
}